package sis.ra.utility;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.utility.InxEntity;

import com.inxight.IMS;
import com.inxight.IMSException;
import com.inxight.IMSResultSizeException;


public class Utils extends Thread{
	private static String propFile ="webapps/cisservlet/cisservlet.properties";
//	private static  String uploadRoot= "C:\\java\\workspace\\TopicTracker1\\www\\upload\\";
	private static String wwwRoot ="C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\cisservlet\\www\\";
	private static  String uploadRoot="C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\cisservlet\\www\\upload\\";
	private static  String urlRoot= "http://localhost:8080/cisservlet/";
	private static 	String startreeRoot="C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\cisservlet\\www\\startreetest\\";  	
	
	
	public static String fetchContentfrURL (String urlString)
	{ 
		urlString=urlString.replace("&amp;", "&");
	//	URLEncoder.encode("http://www.google.com");
	//	System.out.println("fetchContentfrURL: "+urlString);
		String contents="";        
		try{ 
			URL url= new URL (urlString);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();

			if(conn.getResponseCode() < 400 ) 
			{
				BufferedReader in = new BufferedReader (new InputStreamReader ((InputStream)new URL (urlString).getContent()));
				String line="";        
				while ((line = in.readLine()) != null)
				{
					if (line.length()<2) continue;
					contents=contents+line+"\n";
				}
				in.close();
			}
		}catch (Exception e)
		{ e.printStackTrace(); }
		return contents;
	}
	
	public static ArrayList<InxEntity> parseIMSXMLContentList(String xmlfile)
	{
		ArrayList<InxEntity> ens = new ArrayList<InxEntity>();
		ArrayList<String> names=new ArrayList<String> ();
		Document document = null;
		try {
//			document = new SAXBuilder().build(systemId)
//			System.out.println(xmlfile);
			document = new SAXBuilder().build(new StringReader(xmlfile));
		
		Element root = document.getRootElement();
		Namespace inx = root.getNamespace("inxenv");
		Namespace inx2 = Namespace.getNamespace("http://www.inxight.com/ims/response/4.6");

		Element docprocnode = root.getChild("payload",inx).getChild("document-processing",inx2);
		Element entitylist= docprocnode.getChild("extract-entities",inx2).getChild("entities",inx2);
		List children = entitylist.getChildren("entity",inx2);
		for (Object entity : children) {
			Element el = (Element)entity;
			String canonical=el.getAttributeValue("canonical");
			String canonicaltype=el.getAttributeValue("canonical-type");
			String surface=el.getAttributeValue("surface");
			String entitytype=el.getAttributeValue("entity-type");
			String offset=el.getAttributeValue("offset");
			String confidence=el.getAttributeValue("confidence");
			String relevance=el.getAttributeValue("relevance");
//		InxEntity en=new InxEntity("","", "",surface,entitytype,"");
//		public InxEntity(String canoical, String canoicalType, String confidence,
//		String entityType, String id, String offset, String position,
//		String relevance, String surface) {

			InxEntity en=new InxEntity(canonical, canonicaltype, confidence, entitytype, "", offset, "", relevance, surface);	
			ens.add(en);
		}
		
		} catch (Exception e) {
			System.out.println(xmlfile);
			e.printStackTrace();

		}
		return ens;
	}
	
public static HashMap<Integer,InxEntity> parseIMSXMLContentHash(String xmlfile)
{
	HashMap<Integer,InxEntity> ens = new HashMap<Integer,InxEntity>();
	ArrayList<String> names=new ArrayList<String> ();
	Document document = null;
	try {
//		document = new SAXBuilder().build(systemId)
//		System.out.println(xmlfile);
		if (xmlfile.length()<2) return null;
		document = new SAXBuilder().build(new StringReader(xmlfile));
	} catch (Exception e) {
	e.printStackTrace();
	}
	Element root = document.getRootElement();
	Namespace inx = root.getNamespace("inxenv");
	Namespace inx2 = Namespace.getNamespace("http://www.inxight.com/ims/response/4.6");

	Element docprocnode = root.getChild("payload",inx).getChild("document-processing",inx2);
	Element entitylist= docprocnode.getChild("extract-entities",inx2).getChild("entities",inx2);
	List children = entitylist.getChildren("entity",inx2);
	for (Object entity : children) {
		Element el = (Element)entity;
		String canonical=el.getAttributeValue("canonical");
		String canonicaltype=el.getAttributeValue("canonical-type");
		String surface=el.getAttributeValue("surface");
		String entitytype=el.getAttributeValue("entity-type");
		String offset=el.getAttributeValue("offset");
		String confidence=el.getAttributeValue("confidence");
		String relevance=el.getAttributeValue("relevance");
//	InxEntity en=new InxEntity("","", "",surface,entitytype,"");
//	public InxEntity(String canoical, String canoicalType, String confidence,
//	String entityType, String id, String offset, String position,
//	String relevance, String surface) {

		InxEntity en=new InxEntity(canonical, canonicaltype, confidence, entitytype, "", offset, "", relevance, surface);
	
		ens.put(Integer.parseInt(offset),en);
	}				
return ens;
}

	/*
	 * filter using AIS API
	 */
//	public static boolean isWikiEntry(String wikipediaPage)throws Exception
//	{
//		// we're looking for this Wikipedia page:
//	//	final String wikipediaPage = "SAP AG";
//
//		// query AIS REST service
//		String reqStr = "http://cis2:8182/ws/1/connsearch?"
//			+ "infosource=http%3A%2F%2Fen.wikipedia.org%2Fw%2Fapi.php&uri=wikipedia:"
//			+ URLEncoder.encode(wikipediaPage, "UTF-8");
//		// System.out.println(reqStr);
//		Client client = new Client(Protocol.HTTP);
//		Request request = new Request(Method.GET, reqStr);
//		request.getClientInfo().getAcceptedMediaTypes().add(
//			new Preference<MediaType>(MediaType.APPLICATION_JSON));
//		Response response = client.handle(request);
//
//		String respStr = response.getEntity().getText();
//		// System.out.println(respStr);
//		JSONArray items = new JSONArray(respStr);
//
//		// check whether there is (at least) one result
//		boolean pageExists = (items.length() > 0);
//		return pageExists;
//	}

public static boolean isWikiEntry(String wikipediaPage)throws Exception
{
	boolean pageExists=true;
	
	return pageExists;
}
public static String removePunctuation(String original)
{
String newstring=original;
newstring = newstring.replaceAll("[^A-Z0a-z1-9 ]", " ").replace("  ", " ").trim();
return newstring;
}

public static String resultpage(ArrayList<String> pagelists)
{
String resulthtmls="<html><head></head><body>";
for (int i=0;i<pagelists.size();i++)
{
	String filename=pagelists.get(i);
	filename=Utils.removePunctuation(filename);
	if (filename.length()>150) filename=filename.substring(0,150);
	resulthtmls=resulthtmls+"<iframe src=\""+urlRoot+"www/results/"+filename+"\" width=\"1000\" height=\"90\"></iframe> \n";
}
return resulthtmls;
}

public static String passages(String pass)
{
//	System.out.println(pass);
	String pasge="";
	pass=pass.replace("<p >", "<p>");
	pass=pass.replace("<p class=\"cnnInline\">", "<p>");
	while (pass.contains("<p>"))
	{
		pass=pass.substring(pass.indexOf("<p")+3);
//		System.out.println(pass);
		if (pass.startsWith("\n")) pass=pass.substring(1);
		if (pass.substring(0,pass.indexOf("</p>")).length()<2) continue;
		pasge=pasge+pass.substring(0,pass.indexOf("</p>"))+"\n";
	}
//	System.out.println(pasge);
//	if (!pass.contains("<p>")) pasge=pass;
	if (pasge.contains("<BR>")) pasge=pasge.replace("<BR>", "");
	if (pasge.contains("<li>")) pasge=pasge.replace("<li>", "");
//	System.out.println(pasge);
	String [] pas=pasge.split("\n");
	String content="";
	for (int i=0;i<pas.length;i++)
	{
//		System.out.println(pas[i]);
		if (pas[i].length()<2) continue;
		content=content+pas[i].trim()+"\n";
	}
//	System.out.println(content);
	if (content.contains("<B>")) content=content.replace("<B>", "");
	if (content.contains("</B>")) content=content.replace("</B>", "");
	if (content.contains("<b>")) content=content.replace("<b>", "");
	if (content.contains("</b>")) content=content.replace("</b>", "");
	
	return content;
}

public static String getNE(String queryString) {

	String nameentity="";
	byte[] response=null;
	if (queryString.length()<1) {
		System.out.println("queryString, You must enter a query.");
		return "";			
	}

	String host = "localhost"; // assign the IMS server's host
	Integer port = new Integer(55555); // assign the IMS server's port number
	int timeout = 300; // default to 300 seconds (5 mins)
	//TODO: generate random file name;\
	String temp_file_name = System.currentTimeMillis()+"temp.txt";
	String full_file_name = wwwRoot+temp_file_name;
//	System.out.println("queryString "+queryString);
	Utils.writeFile(full_file_name, queryString);

//	System.out.println("util read file: "+Utils.readFile(full_file_name));
	//	Logger.msg("path: "+full_file_name);
	//        out.println("temp_file_name="+temp_file_name);
	String config_file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
	+ "<inxight:inxight-xml xmlns:inxight=\"http://www.inxight.com/envelope\">\n"
	+ "<inxight:header>   \n "
	+ "<inxight:description>Extract Entities</inxight:description>    \n"
	+ "<inxight:sender>Sender</inxight:sender>  \n"
	+ "  <inxight:receiver>Receiver</inxight:receiver> \n"
	+ " </inxight:header> \n "
	+ "<inxight:payload>  \n "
	+ " <document-processing xmlns=\"http://www.inxight.com/ims/request/4.6\">\n"
	+ "      <document>        \n"
	+ "<uri name=\""+urlRoot+"www/"
	+ temp_file_name
	+ "\"/>\n"
	+ "      </document>   \n"
	+ "   <extract-entities language=\"english\" conjecture=\"true\" aliasing=\"true\" relevance-rank=\"true\""
	+ " doc-image=\"true\" use-advanced-parsing=\"true\">\n"
	+ "</extract-entities>\n" + "</document-processing>\n"
	+ "</inxight:payload>\n" + "</inxight:inxight-xml>\n";

//    out. println("<html>");
//    out.println("<head><title>Named Entity</title></head>");
//    out.println("<body><h1>NE:</h1>");

	byte[] buffer = config_file.getBytes();
	//        out.println("file  "+filename +"</br>");
	//        out.println("config file content: "+ config_file +"</br>");

	try {
	// connect to the IMS server
	IMS ims = new IMS(host, port.intValue());

	// send one request to the server
	long maxBufSize = 10000000; // Maximum number of bytes client will accept from server.

	
	response = ims.SubmitRPCTaskBuffer(IMS.TASK_IMS, buffer,
			IMS.PLACE_AT_TAIL, maxBufSize, timeout);
		System.out.println(response);
//		Logger.msg("Response is :"+new String(response));

//		System.out.println(new String(response, "UTF-8"));

	//            out.println(new String(response, "UTF-8"));
	//            System.out.print("\n");
	//            out.print("\n");
	ims.Disconnect();
	//TODO: remove temp file

	
} catch (IMSException ce) {
	System.out.println("} catch (IMSException ce ){");
	System.out.println(ce.toString());

} catch (java.net.UnknownHostException ce) {
	System.out.println("} catch (java.net.UnknownHostException ce ){");
	System.out.println(ce.toString());
} catch (java.io.IOException ce) {
	ce.printStackTrace();	
} catch (IMSResultSizeException ce) {
	ce.printStackTrace();
	}
File tmp=new File (full_file_name);
if (tmp.exists()) tmp.delete();
//        out.close();
nameentity=new String (response);
return nameentity;
}
	

public static boolean MakeDir(String strDirectoy) {
try {
	//	     String strDirectoy ="test";
	//	     String strManyDirectories="dir1/dir2/dir3";

	// Create one directory
	boolean success = (new File(strDirectoy)).mkdir();
	if (success) {
//			System.out.println("Directory: " + strDirectoy + " created");

	}
	return success;

	// Create multiple directories
	//	     success = (new File(strManyDirectories)).mkdirs();
	//	     if (success) {
	//	       System.out.println("Directories: " + strManyDirectories + " created");
	//	     }

} catch (Exception e) {//Catch exception if any
	System.err.println("Error: " + e.getMessage());
}
return false;
}

	public static LinkedList<String> sortHashMapbyScore(HashMap<String, Double> sentenseweight)
	{
		LinkedList<String> newsentenseweight=new LinkedList<String>();
//     	List<String> mapKeys=new ArrayList<String> (sentenseweight.keySet());
		List<Double> mapValues = new ArrayList<Double>(sentenseweight.values());
		TreeSet<Double> sortedSet=new TreeSet<Double>(mapValues);
		Object [] sortedArray=sortedSet.toArray();

		for (int i=sortedArray.length-1;i>=0;i--)
		{
			Double value=(Double)sortedArray[i];
			Iterator<String> keys=sentenseweight.keySet().iterator();
			while (keys.hasNext())
			{
				String key=keys.next();
				if (sentenseweight.get(key).equals(value))
				{
//					System.out.println("=="+key+" "+value);
					newsentenseweight.add(key+"\t"+value);
				}
			}
		}
		return newsentenseweight;
	}
public static HashMap<String, Double> cissummary(String filename)
{
HashMap<String, Double> sentenseweight=new HashMap<String, Double>();
HashMap<String, Double> newsentenseweight=new HashMap<String, Double>();
String summerystring="";
String projroot=wwwRoot+"\\summary\\";
String sumcommand="\""+projroot+"sum\\test_summarizer.exe\" " +
		"-d \""+projroot+"lang\"" +
		" -k 735442535d524e -html -lf \""+projroot+"lang\\license.dat\" " +
		"-sf 0.2 \""+projroot+"sum\\"+filename+"\""; 

//System.out.println(sumcommand);
try{
Process p = Runtime.getRuntime().exec(sumcommand);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
//     BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

String s="";
while ((s = stdInput.readLine()) != null)   
{
//  	System.out.println(s);
	if (!s.equals("[pid, psid, score]: string")) continue;
	while ((s = stdInput.readLine()) != null)   
    {
		if (s.startsWith("There is a total of")) break;
		summerystring=summerystring+s+"\t";
		String sen=s.split(":")[1].trim();
		Double score=Double.parseDouble(s.split(":")[0].trim().split(",")[2].replace("]", ""));
//      		System.out.println("score: "+score);
		sentenseweight.put(sen, score);
//      		System.out.println(s);        		
    }       		
}
//     while ((s = stdError.readLine()) != null)   System.out.println(s);

List<String> mapKeys=new ArrayList<String> (sentenseweight.keySet());
List<Double> mapValues = new ArrayList<Double>(sentenseweight.values());
TreeSet<Double> sortedSet=new TreeSet<Double>(mapValues);
Object [] sortedArray=sortedSet.toArray();

for (int i=sortedArray.length-1;i>=0;i--)
{
	String key=mapKeys.get(mapValues.indexOf(sortedArray[i]));
	double score=sentenseweight.get(key);
//		System.out.println(score);
	newsentenseweight.put(key, score);
}

}
catch (IOException e) {
    System.out.println("exception happened - here's what I know: ");
    e.printStackTrace();
//    System.exit(-1);s
}
return newsentenseweight;
}


/**
* read a file on server
* @param file
* @return
*/
public static String readFile(String file) {
StringBuffer buf = new StringBuffer();
try {
	DataInputStream in = new DataInputStream(new FileInputStream(file));
	BufferedReader br = new BufferedReader(new InputStreamReader(in));
	String strLine;
	//Read File Line By Line
	while ((strLine = br.readLine()) != null) {
		// Print the content on the console
		//	    	      System.out.println (strLine);
		buf.append(strLine);
		buf.append("\n");
	}
	//Close the input stream
	in.close();
} catch (Exception e) {//Catch exception if any
	System.err.println("Error: " + e.getMessage());
}
return buf.toString();
}

public static boolean writeToFile(String fileName, String dataLine,  boolean isAppendMode, boolean isNewLine) {
DataOutputStream dos;
if (isNewLine) {
      dataLine = "\n" + dataLine;
    }

    try {
      File outFile = new File(fileName);
      if (isAppendMode) {
    	  dos = new DataOutputStream(new FileOutputStream(fileName, true));
      } else {
    	  dos = new DataOutputStream(new FileOutputStream(outFile));
      }
      dos.writeBytes(dataLine);
      dos.close();
    } catch (FileNotFoundException ex) {
      return (false);
    } catch (IOException ex) {
      return (false);
    }
    return (true);

  }

/**
* write a file on server
* @param file
* @param content
*/
public static void writeFile(String file, String content) {
try {
	// Create file 
	FileWriter fstream = new FileWriter(file);
	BufferedWriter out = new BufferedWriter(fstream);
//	out.write("\n");
	out.write(content);
	//Close the output stream
	out.close();
} catch (Exception e) {//Catch exception if any
	e.printStackTrace();
}
}

public static String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date date = new Date();
return dateFormat.format(date);
} 

public static void RunCommand(String cmd) {

String s = null;
System.out.println(cmd);
try {
     Process p = Runtime.getRuntime().exec(cmd);
    
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    while ((s = stdInput.readLine()) != null)   System.out.println(s);
    while ((s = stdError.readLine()) != null)   System.out.println(s);
}
catch (IOException e) {
    e.printStackTrace();    
}
}
	
    
    public static void main(String[] args) {
    	System.out.println("test'test".replace("'", "%27"));
	}
}
